package com.yiqi.liebang.feature.home.view;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.baiiu.filter.ClassifyBo;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.RatingBar;
import com.yiqi.liebang.common.widget.ShareBottomDialog;
import com.yiqi.liebang.common.widget.dialog.BaseAnimatorSet;
import com.yiqi.liebang.common.widget.dialog.BounceTopEnter;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.entity.enums.OrderType;
import com.yiqi.liebang.feature.home.contract.ClassifyContract;
import com.yiqi.liebang.feature.home.dependencies.classsify.ClassifyPresenterModule;
import com.yiqi.liebang.feature.home.dependencies.classsify.DaggerClassifyComponent;
import com.yiqi.liebang.feature.order.view.adapter.StepAdapter;
import com.yiqi.liebang.feature.setting.view.ServiceAgreementActivity;
import com.yiqi.liebang.framework.DataServer;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ReservationTopicActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener,
        ClassifyContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_step)
    RecyclerView mRvStep;

    @BindView(R.id.tv_topic_type)
    TextView mTvTopicType;
    @BindView(R.id.tv_topic_title)
    TextView mTvTopicTitle;
    @BindView(R.id.tv_topic_help)
    TextView mTvTopicHelp;
    @BindView(R.id.tv_topic_date)
    TextView mTvTopicDate;
    @BindView(R.id.star)
    RatingBar mStar;
    @BindView(R.id.tv_topic_prize)
    TextView mTvTopicPrize;
    @BindView(R.id.tv_topic_prize_ago)
    TextView mTvTopicPrizeAgo;


    String mTopicId;
    @BindView(R.id.edt_topic_desc)
    EditText mEdtTopicDesc;
    @BindView(R.id.edt_topic_question)
    EditText mEdtTopicQuestion;
    @BindView(R.id.tv_topic_prize_order)
    TextView mTvTopicPrizeOrder;
    @Inject
    ClassifyContract.Presenter mPresenter;
    @BindView(R.id.cbx_lottery_daleto)
    CheckBox mCbxLotteryDaleto;
    private boolean isNext = true;
    private UserCenterBo.TopicBean mData;

    private String classifyLabels;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_reservation_topic;
    }


    @Override
    protected void initInjector() {
        DaggerClassifyComponent.builder().classifyPresenterModule(new ClassifyPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
//        mToolbar.inflateMenu(R.menu.menu_share);
        initToolBar(mToolbar, "话题详情", true, true);
        mToolbar.setOnMenuItemClickListener(this);
    }

    @Override
    protected void initData() {
        initStepView();

        mTopicId = this.getIntent().getStringExtra("topic");
        if (!TextUtils.isEmpty(mTopicId)) {
            getTopicDetail(mTopicId);
        }

        classifyLabels=this.getIntent().getStringExtra("ask_lables");
    }


    private void initStepView() {
        mRvStep.setLayoutManager(new GridLayoutManager(this, 4));
        StepAdapter stepAdapter = new StepAdapter();
        stepAdapter.bindToRecyclerView(mRvStep);
        stepAdapter.setNewData(DataServer.getOrderStautYYFK());

    }


    @OnClick(R.id.btn_topic_order)
    public void onViewClicked() {
        if (mData == null) finish();

        if (TextUtils.isEmpty(mEdtTopicDesc.getText().toString())) {
            showMsg("请输入要请教的问题！");
            return;
        }

        if (TextUtils.isEmpty(mEdtTopicQuestion.getText().toString())) {
            showMsg("请输入备注信息！");
            return;
        }
        if (!isNext) {
            T.showShort("请同意猎帮服务协议！");
            return ;
        }
        Intent intent = new Intent(this, PayActivity.class);
        intent.putExtra("ask_prize", mData.getTopicPrice());
        intent.putExtra("ask_userid", mData.getUserAccountFormMap().getUserUid());
        intent.putExtra("topicAskQuestion", mEdtTopicDesc.getText().toString());
        intent.putExtra("askIntroduction", mEdtTopicQuestion.getText().toString());
        intent.putExtra("ask_lables", classifyLabels);
        intent.putExtra("topicId", mData.getId());
        intent.putExtra("order_type", OrderType.HUATI.getIndex());
        intent.putExtra("ask_lables", classifyLabels);
        startActivity(intent);
        finish();
    }

    private void getTopicDetail(String id) {
        AM.api().createApiService(HomeApi.class)
                .getTopicDetail(id)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<UserCenterBo.TopicBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(UserCenterBo.TopicBean data) {
                        mData = data;
                        if (mData != null) {
                            mPresenter.getUserLabelByType(mData.getUserAccountFormMap().getUserUid());
                            mTvTopicType.setText(mData.getServiceType() == 0 ? "线下约见" : "全国通话");
                            mTvTopicTitle.setText(mData.getTopicName());
                            mTvTopicHelp.setText("帮助过" + mData.getUserAccountFormMap().getHelpNum() + "人");
                            mTvTopicDate.setText(mData.getServiceTime() + "小时/次");
                            mTvTopicPrize.setText(mData.getTopicPrice() + "");
                            mTvTopicPrizeAgo.setText(mData.getOriginalPrice() + "元/次");
                            mTvTopicPrizeOrder.setText(mData.getTopicPrice() + "元");
                            mStar.setStar(mData.getUserAccountFormMap().getScore());
                        }
                        dismissLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        showMsg(e.getMessage());
                        dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        BaseAnimatorSet mBasIn = new BounceTopEnter();
        final ShareBottomDialog dialog = new ShareBottomDialog(this, null, false);
        dialog.showAnim(mBasIn)
                .show();
        return false;
    }

    @Override
    protected void initEvent() {
        mCbxLotteryDaleto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isNext = isChecked;
            }
        });
    }

    @Override
    public void showUserSuccess(String classifyBos) {
//        classifyLabels = classifyBos;
    }

    @Override
    public void showUserError(String classifyBos) {

    }

    @Override
    public void showSuccess(List<ClassifyBo> classifyBos) {

    }

    @Override
    public void showExpert(List<SearchFriendBo.UserFriendBean> classifyBos) {

    }

    @Override
    public void showEmpty() {

    }

    @Override
    public void showError(String ex) {

    }


    @OnClick(R.id.btn_to_xieyi)
    public void onViewXYClicked() {
        startActivity(new Intent(this, ServiceAgreementActivity.class));
    }
}